(() => {
    const getTable = () => {
        const table = document.getElementsByTagName("table")[0];
        const rows = Array.from(table.getElementsByTagName("tr"));
        return rows.map((e) => e.innerText.split("\n"));
    }

    // const submitButton = document.createElement("button");
    // document.body.appendChild(submitButton);
    // submitButton.style.position = "fixed";
    // submitButton.style.left = 0;
    // submitButton.style.bottom = 0;
    // submitButton.innerText = "测试按钮"
    // const getSubmitButton = () => submitButton

    const getSubmitButton = () => document.querySelector("button.ant-btn-primary");
    const getId = () => document.getElementById("text-undefined").innerText;
    const btn = document.createElement("button");
    btn.className = "ant-btn-primary";
    btn.innerText = "开始检测";
    btn.style.position = "fixed";
    btn.style.right = "100px";
    btn.style.bottom = "100px";
    document.body.appendChild(btn);
    let card;
    let time;
    let event = false;
    let current;
    const getData = () => {
        time = setTimeout(() => {
            try {
                const id = getId();
                current = id;
                // if (current == id) {
                //     if (event) getData();
                //     return;
                // }
                fetch(`http://bug.hblzhd.com:14520/script/${id}/get`).then((e) => {
                    return e.json()
                }).then((data) => {
                    // current = id;
                    if (!data.id || !data.data) {
                        card.innerText = "暂无数据";
                        return;
                    }
                    const table = JSON.parse(data.data)
                    // card.innerHTML = JSON.stringify(data);
                    card.innerHTML = table.map(e => `<div>${JSON.stringify(e)}</div>`);
                }).finally(() => {
                    if (event) getData()
                })
            } catch (e) {
                if (event) getData();
                return;
            }
        }, 1000);
    }
    const click = () => {
        if (!current) return;
        fetch(`http://bug.hblzhd.com:14520/script/${current}/set/${JSON.stringify(getTable())}`);
    }
    const setButtonEvent = () => {
        const btn = getSubmitButton();
        if (!btn) {
            return setTimeout(() => {
                setButtonEvent();
            }, 500);
        }
        btn.addEventListener("click", click);
    }
    const start = () => {
        btn.className = "ant-btn-primary";
        btn.innerText = "停止";
        card = document.createElement("div")
        card.style.background = "#fff";
        card.style.left = "100px";
        card.style.bottom = "100px";
        card.style.position = "fixed";
        document.body.appendChild(card);
        setButtonEvent();
        getData();
    }
    const close = () => {
        btn.className = "ant-btn-primary";
        btn.innerText = "开始检测";
        clearTimeout(time);
        getSubmitButton()?.removeEventListener("click", click);
        document.body.removeChild(card);
    }
    btn.onclick = () => {
        event = !event;
        if (event) start();
        else close();
    }
})()